﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;

namespace Common.Util
{
    public class DesEncryptor
    {
        /// <summary>
        /// Encrypted by tripleDES
        /// </summary>
        /// <param name="plainText"></param>
        /// <param name="key"></param>
        /// <param name="IV"></param>
        /// <returns></returns>
        public static string Encrypt(string plainText, string key, string IV)
        {
            try
            {
                string result = string.Empty;
                TripleDES edes = new TripleDESCryptoServiceProvider();
                edes.Mode = CipherMode.CBC;
                byte[] bytes = Encoding.UTF8.GetBytes(plainText);
                byte[] buffer2 = Encoding.ASCII.GetBytes(key);
                byte[] buffer3 = Encoding.ASCII.GetBytes(IV);
                edes.Key = buffer2;
                edes.IV = buffer3;
                ICryptoTransform transform = edes.CreateEncryptor(edes.Key, edes.IV);
                using (MemoryStream stream = new MemoryStream())
                {
                    stream.Write(bytes, 0, bytes.Length);
                    using (new CryptoStream(stream, transform, CryptoStreamMode.Write))
                    {
                        result = Convert.ToBase64String(stream.ToArray());
                    }
                }
                return result;

            }
            catch(Exception err)
            {
                throw err;
            }
           
        }

        /// <summary>
        /// Decrypted by tripleDES 
        /// </summary>
        /// <param name="cliperText"></param>
        /// <param name="key"></param>
        /// <param name="IV"></param>
        /// <returns></returns>
        public static string Decrypt(string cliperText, string key, string IV)
        {
            try
            {
                string result = string.Empty;
                TripleDES edes = new TripleDESCryptoServiceProvider();
                edes.Mode = CipherMode.CBC;
                byte[] buffer = Convert.FromBase64String(cliperText);
                byte[] bytes = Encoding.ASCII.GetBytes(key);
                byte[] buffer3 = Encoding.ASCII.GetBytes(IV);
                edes.Key = bytes;
                edes.IV = buffer3;
                ICryptoTransform transform = edes.CreateDecryptor(edes.Key, edes.IV);
                using (MemoryStream stream = new MemoryStream())
                {
                    stream.Write(buffer, 0, buffer.Length);
                    new CryptoStream(stream, transform, CryptoStreamMode.Write);
                    byte[] buffer4 = stream.ToArray();
                    result = Encoding.UTF8.GetString(buffer4);
                }
                return result;
            }
            catch(Exception err)
            {
                throw err;
            }
            
        }

    }
}
